window.onload = function () {
    /*axios.post("http://localhost:8080/user/common").then(res => {
        var username = document.getElementById("username");
        if(res.data.state){
            var admin = res.data.admin;
            username.innerText = admin.username;
        }
    })*/
    $.ajax({

    })
    //vue实例
    var show = new Vue({
        el: "#select",
        data: {
            lists:[{
            }]
        },
        methods: {
            showAllUser(){
                var that = this;
                $.ajax({
                    url:"http://localhost:8080/Barbr/login?action=show",
                    type:"get",
                    cache:false,
                    data:{},
                    dataType:"JSON",
                    success(data){
                        that.lists = data;
                        /*$.each(data,function (i,list){

                        });*/
                    },
                    error(XMLHttpRequest, textStatus, errorThrown){
                        alert("请求失败");
                        alert(XMLHttpRequest.status);
                        alert(XMLHttpRequest.readyState);
                        alert(textStatus);
                    }
                });
              /*  axios.post("http://localhost:8080/Barbr/login?action=show").then(res => {
                    console.log(res.data);
                    if(res.data.state) {
                        this.lists = res.data.users;
                        console.log(res.data.users);
                        for (var i = 0; i < this.lists.length; i++) {
                            switch (this.lists[i].type) {
                                case '0':
                                    this.lists[i].type = "剪发";
                                    break;
                                case '1':
                                    this.lists[i].type = "洗发";
                                    break;
                                case '2':
                                    this.lists[i].type = "护理";
                                    break;
                                case '3':
                                    this.lists[i].type = "补发根";
                                    break;
                                case '4':
                                    this.lists[i].type = "染发";
                                    break;
                                case '5':
                                    this.lists[i].type = "黑油";
                                    break;
                                case '6':
                                    this.lists[i].type = "烫发+剪发";
                                    break;
                                case '7':
                                    this.lists[i].type = "烫发+染发";
                                    break;
                                case '8':
                                    this.lists[i].type = "洗发+染发";
                                    break;
                                case '9':
                                    this.lists[i].type = "剪发+黑油";
                                    break;
                                case '10':
                                    this.lists[i].type = "剪发+染发";
                                    break;
                            }
                        }
                    }
                })*/
            }
        }
    });
    show.showAllUser();
}
//删除用户
function deleteCurrentRow(obj,tellphone) {
    $.ajax({
       url:"http://localhost:8080/Barbr/dispose?action=delete&tellphone="+tellphone,
       type:"get",
       cache:false,
       data:{},
       success(data){
         var tr = obj.parentNode.parentNode;
         var tbody = tr.parentNode;
         tbody.removeChild(tr);
         console.log(obj);
        },
        error(){
           alert("err")
        }
    });
    //show.lists = data1;
    /*axios.post("http://localhost:8080/Barar/dispose",{tellphone:tellphone,action:deleteUser}).then(res => {
        alert(res.data.msg);
        if(res.data.state){
            var tr = obj.parentNode.parentNode;
            var tbody = tr.parentNode;
            tbody.removeChild(tr);
            console.log(obj)
            //只剩行首时删除表格
        }
    })*/
}
//减少次数
function mreduce(obj,times,tellphone,id) {
    if(times === '0' || times < '0'){
        alert("当前次数不足，请充值！");
        return false;
    }
    let op = $("#"+id+" option:selected").val();    //得到选择的几级理发师
    if (op == "选择理发师"){
        alert("请先选择理发师~");
        return false;
    }

    /*let options = document.getElementById(id);
    alert(options.options[options.selectedIndex].value);*/

    var kind = obj;
    var flag = confirm("确定减少"+kind+"次数？");
    if(flag){
        $.ajax({
            url:"http://localhost:8080/Barbr/dispose?action=mreduce&type="+kind+"&tellphone="+tellphone+"&times="+times+"&level="+op,
            type:"get",
            cache:false,
            data:{},
            dataType: "text",
            success(data){
                alert(data);
                location.href="/Barbr/html/MembershipManagement.jsp";
            },
            error(XMLHttpRequest, textStatus, errorThrown){
                alert("请求失败");
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus);
            }
        });
        /*axios.post("http://localhost:8081/admin/timesReduce",{type:kind,tellphone:tellphone}).then(res => {
          if(res.data.state){
              alert(kind+"次数-1");
              location.href = "../html/MembershipManagement.jsp";
          }else{
              alert(type+"操作失败！");
          }
        })*/
    };
    //show.lists = data2;

}
//未访问服务的用户搜索
function searchUser(){
    setTimeout(function(){//因为是即时查询，需要用setTimeout进行延迟，让值写入到input内，再读取
        var storeId = document.getElementById('select');//获取table的id标识
        var selectTel = document.getElementById('selectTel');//获取table的id标识
        var rowsLength = storeId.rows.length;//表格总共有多少行
        var key = selectTel.value;//获取输入框的值
        var searchCol = 2;//要搜索的哪一列，这里是第一列，从0开始数起
        var seleCol = 1;
        if(!isNaN(key)){
            for(var i=1;i<rowsLength;i++){//按表的行数进行循环，本例第一行是标题，所以i=1，从第二行开始筛选（从0数起）
                var searchText = storeId.rows[i].cells[searchCol].innerHTML;//取得table行，列的值
                if(searchText.match(key)){//用match函数进行筛选，如果input的值，即变量 key的值为空，返回的是ture，
                    storeId.rows[i].style.display='';//显示行操作，
                }else{
                    storeId.rows[i].style.display='none';//隐藏行操作
                }
            }
        }
        else{
            for(var q=1;q<rowsLength;q++){//按表的行数进行循环，本例第一行是标题，所以i=1，从第二行开始筛选（从0数起）
                var searchVal = storeId.rows[q].cells[seleCol].innerHTML;//取得table行，列的值
                if(searchVal.match(key)){//用match函数进行筛选，如果input的值，即变量 key的值为空，返回的是ture，
                    storeId.rows[q].style.display='';//显示行操作，
                }else{
                    storeId.rows[q].style.display='none';//隐藏行操作
                }
            }
        }
    },200);//200为延时时间
}
